package com.totofgeek.contactsboard.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

import com.totofgeek.contactsboard.ContactActivity;
import com.totofgeek.contactsboard.R;
import com.totofgeek.contactsboard.widget.outils.AbstractWidgetProvider;
import com.totofgeek.contactsboard.widget.outils.WidgetService;

public class StackWidgetProvider extends AbstractWidgetProvider
{
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.totofgeek.contactsboard.widget.AbstractWidgetProvider#onUpdate(android.content.Context, android.appwidget.AppWidgetManager, int[])
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
	{
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		Intent intent = null;
		Intent appelActivte = null;
		RemoteViews rv = null;
		Intent paramIntent = null;
		PendingIntent pendingIntent = null;
		PendingIntent toastPendingIntent = null;

		for (int i = 0; i < appWidgetIds.length; ++i)
		{
			// Sets up the intent that points to the StackWidgetService that will
			// provide the views for this collection.
			intent = new Intent(context, WidgetService.class);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

			// When intents are compared, the extras are ignored, so we need to embed the extras
			// into the data so that the extras will not be ignored.
			intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

			rv = new RemoteViews(context.getPackageName(), R.layout.widget_stack);
			rv.setRemoteAdapter(R.id.stackWidgetView, intent);

			// The empty view is displayed when the collection has no items. It should be a sibling
			// of the collection view.
			rv.setEmptyView(R.id.stackWidgetView, R.id.stackWidgetEmptyView);

			// This section makes it possible for items to have individualized behavior.
			// It does this by setting up a pending intent template. Individuals items of a collection
			// cannot set up their own pending intents. Instead, the collection as a whole sets
			// up a pending intent template, and the individual items set a fillInIntent
			// to create unique behavior on an item-by-item basis.
			paramIntent = new Intent(context, StackWidgetProvider.class);

			// Set the action for the intent.
			// When the user touches a particular view, it will have the effect of
			// broadcasting TOAST_ACTION.
			paramIntent.setAction(StackWidgetProvider.PARAM_ACTION);
			paramIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

			toastPendingIntent = PendingIntent.getBroadcast(context, 0, paramIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			rv.setPendingIntentTemplate(R.id.stackWidgetView, toastPendingIntent);

			appelActivte = new Intent(context, ContactActivity.class);
			pendingIntent = PendingIntent.getActivity(context, 0, appelActivte, 0);
			rv.setOnClickPendingIntent(R.id.bouton_activite, pendingIntent);

			// update widget
			appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
		}
	}
}